#include <iostream>
#include <vector>

using namespace std;

// 1493. 删掉一个元素以后全为 1 的最长子数组
// https://leetcode.cn/problems/longest-subarray-of-1s-after-deleting-one-element/description/

class Solution {
public:
    int longestSubarray(vector<int>& nums) {
        int ans = 0;
        int n = nums.size();
        int zeroCnt = 0;
        int left = 0;
        for (int i = 0; i < n; i++)
        {
            if(nums[i] == 0){
                zeroCnt++;
            }
            while(zeroCnt >= 2){
                if(nums[left] == 0){
                    zeroCnt--;
                }
                left++;
            }
            ans = max(ans, i - left);
        }
        return ans;
    }
};

int main()
{
    vector<int> nums{0, 1, 1, 1, 0, 1, 1, 0, 1};
    Solution solution{};
    solution.longestSubarray(nums);
    return 0;
}